		include	Common.asm

; ==========================================================
DataSeg		segment para public 'data'

	NullString	db StringLength dup (0)
	StrBuffer	db StringLength dup (0)
	ValueTen	dw 10

DataSeg		ends

; ==========================================================
CodeSeg		segment para public 'code'
		assume	ds: DataSeg


; ----------------------------------------------------------
; GetClockTick - Get system clock tick
; ----------------------------------------------------------
; Input:	none
; Output:
;	cx:dx	clock count
; ----------------------------------------------------------
GetClockTick	proc

	push	ax

	mov	ah, 0			; INT 1A - AH = 00h CLOCK - GET TIME OF DAY
	int	1ah

	pop	ax

	ret

GetClockTick	endp


; ----------------------------------------------------------
; ExitProgram - Exits Program
; ----------------------------------------------------------
; Input:
;	al	Exit code
; Output:
; ----------------------------------------------------------
ExitProgram	proc

	mov	ah, 4ch
	int	21h			; ah = 4ch - Terminate program, al = Exit code
	ret

ExitProgram	endp


; ----------------------------------------------------------
; IsCursorVisible - Returns whether cursor is visible
; ----------------------------------------------------------
; Input:	none
; Output:
;	ax	1 if visible, 0 if hidden
; ----------------------------------------------------------
IsCursorVisible	proc

	push	cx
	push	dx

	; cursor blinks every 4(2^2) tick
	call	GetClockTick
	shr	dx, 1
	shr	dx, 1

	mov	ax, 0
	test	dx, 1
	jz	CursorNotVisible

	mov	ax, 1
CursorNotVisible:
	pop	dx
	pop	cx
	ret

IsCursorVisible	endp


; ----------------------------------------------------------
; NumberToString - Converts number to string (unsigned word)
; ----------------------------------------------------------
; Input:
;	ax	Number
;	ds:si	Pointer to destination string
; Output:	none
; ----------------------------------------------------------
NumberToString	proc

	push	ax
	push	cx
	push	dx
	push	di

	mov	cx, 0
	lea	di, StrBuffer

NumberToStringLoop:
	mov	dx, 0
	div	ValueTen

	add	dl, '0'
	mov	[di], dl

	inc	cx
	cmp	ax, 0
	je	NumberToStringLoopExit

	inc	di
	jmp	NumberToStringLoop

NumberToStringLoopExit:
	cmp	cx, 0
	jne	NumberToStringNonZero

	inc	cx			; make digit count 1 if number is 0
NumberToStringNonZero:

	push	si

NumberToStringReverseStringLoop:
	mov	al, [di]
	mov	[si], al
	inc	si
	dec	di
	loop	NumberToStringReverseStringLoop

	mov	byte ptr [si], 0
	pop	si

	pop	di
	pop	dx
	pop	cx
	pop	ax

	ret

NumberToString	endp


; ----------------------------------------------------------
; StringToNumber - Converts string to number (unsigned word)
; ----------------------------------------------------------
; Input:
;	ds:si	Pointer to source string
; Output:
;	ax	Number
; ----------------------------------------------------------
StringToNumber	proc

	push	dx
	push	si

	mov	ax, 0

StringToNumberLoop:
	cmp	byte ptr [si], 0
	je	StringToNumberLoopExit

	mul	ValueTen
	mov	dl, [si]

	cmp	dl, '0'
	jb	StringToNumberLoopExit

	cmp	dl, '9'
	ja	StringToNumberLoopExit

	sub	dl, '0'
	mov	dh, 0
	add	ax, dx

	inc	si
	jmp	StringToNumberLoop

StringToNumberLoopExit:

	pop	si
	pop	dx

	ret

StringToNumber	endp


; ----------------------------------------------------------
; StrCopy - Copys String
; ----------------------------------------------------------
; Input:
;	ds:si	Pointer to source string
;	ds:di	Pointer to dest string
; Output:
; ----------------------------------------------------------
StrCopy		proc

	push	cx
	push	es

	mov	cx, ds
	mov	es, cx
	mov	cx, StringLength
	rep	movsb
	
	pop	es
	pop	cx
	
	ret

StrCopy		endp


; ----------------------------------------------------------
; StrCatChar - Concatenates a character to a string
; ----------------------------------------------------------
; Input:
;	al	Character to concatenate
;	ds:di	Pointer to dest string
; Output:
; ----------------------------------------------------------
StrCatChar	proc

	push	cx
	push	es

	push	ax
	mov	cx, ds
	mov	es, cx
	mov	cx, StringLength
	mov	al, 0
	repnz	scasb
	pop	ax

	dec	di
	mov	[di], al
	inc	di
	mov	byte ptr [di], 0

	pop	es
	pop	cx
	
	ret

StrCatChar	endp


; ----------------------------------------------------------
; Beep - Plays a default beep
; ----------------------------------------------------------
; Input:	none
; Output:	none
; ----------------------------------------------------------
Beep		proc

	push	ax
	push	dx

	mov	ah, 2
	mov	dl, 7
	int	21h

	pop	dx
	pop	ax

	ret

Beep		endp

CodeSeg		ends

		end

